<?php

use app\service\auth;
use Illuminate\Database\Capsule\Manager as DB;

class Dianpu_ApplyController extends CommonController
{
    #[查看申请]
    public function detailAction()
    {
        try {
            $userinfo = auth::userinfo();
            $zmd_cg_money = configModel::where('type', 1)->where('key','zmd_cg_money')->value('value');
            json(['code'=> 1, 'msg' =>'ok','is_sf'=>$userinfo['is_sf'],'zmd_cg_money'=>$zmd_cg_money,'zmd_yj_money'=>'30万','flag' => true]);
        }catch (Exception $e){
            $zmd_yj_money = configModel::where('type', 1)->where('key', 'zmd_yj_money')->value('value');
            if($this->user->total_consume_num >= $zmd_yj_money){
                $flag = true;
            }else{
                $flag = false;
            }
            json(['code' => 300, 'msg' => '申请不存在','total_consume_num'=>$this->user->total_consume_num,'zmd_yj_money'=> $zmd_yj_money,'flag'=>$flag,'is_sf'=>0]);
            exit;
        }
    }

    #[修改申请]
//    public function editAction()
//    {
//        if (!$this->isPost()){
//            json(['code' => 0, 'msg' => '必须POST']);
//            exit;
//        }
//        $id = $this->get('id');
//        if (!is_numeric($id)){
//            json(['code' => 0, 'msg' => '非法请求', $id]);
//            exit;
//        }
//        $params = $this->postData;
//        try {
//            Validate::check($params, [
//                'name' => [
//                    'role' => 'required|maxlength:20|minlength:4',
//                    'msg' => '店铺名称',
//                ],
//                'realname' => [
//                    'role' => 'required|maxlength:10|minlength:2',
//                    'msg' => '法人代表姓名',
//                ],
//                'company_name' => [
//                    'role' => 'required|maxlength:20|minlength:2',
//                    'msg' => '公司名称',
//                ],
//                'phone' => [
//                    'role' => 'required|maxlength:20|minlength:7',
//                    'msg' => '联系电话',
//                ],
//                'area_name' => [
//                    'role' => 'required',
//                    'msg' => '商家所在地区',
//                ],
//                'address' => [
//                    'role' => 'required',
//                    'msg' => '商家详细地址',
//                ],
//                'company_bank' => [
//                    'role' => 'required',
//                    'msg' => '对公帐户开户行',
//                ],
//                'company_bank_name' => [
//                    'role' => 'required',
//                    'msg' => '对公帐户名称',
//                ],
//                'company_bank_card' => [
//                    'role' => 'required',
//                    'msg' => '对公帐户帐号',
//                ],
//                'company_license_image' => [
//                    'role' => 'required',
//                    'msg' => '企业营业执照',
//                ],
//                'company_representative_idcard_front_image' => [
//                    'role' => 'required',
//                    'msg' => '法定代表人身份证头像面照片',
//                ],
//                'company_representative_idcard_reverse_image' => [
//                    'role' => 'required',
//                    'msg' => '法定代表人身份证国徽面照片',
//                ],
//                //                'company_representative_idcard_hand_image' => [
//                //                    'role' => 'required',
//                //                    'msg' => '法定代表人身份证手持照片',
//                //                ],
//            ]);
//            if(
//                \app\modules\Admin\models\dianpuapplyModel::query()->where('company_name', '=', $params['company_name'])->where('id', '<>', $id)->exists()
//                ||
//                dianpuModel::query()->where('company_name', '=', $params['company_name'])->exists()
//            ){
//                throw new Exception('企业名称已经被占用', 30001);
//            }
//            if(isset($params['name'])){
//                if(
//                    \app\modules\Admin\models\dianpuapplyModel::query()->where('name', '=', $params['name'])->where('id', '<>', $id)->exists()
//                    ||
//                    \app\modules\Admin\models\dianpuModel::query()->where('name', '=', $params['name'])->exists()
//                ){
//                    throw new Exception('店铺名称已经被占用', 30001);
//                }
//            }else{
//                $params['name'] = $params['company_name'].'的店铺';
//            }
//            $apply = \app\modules\Admin\models\dianpuapplyModel::query()->findOrFail($id);
//            if($apply->member_id !== $this->user->id){
//                throw new Exception('无法修改别人的申请', 30001);
//            }
//            if($apply->status === 1){
//                throw new Exception('申请已经处理，不能修改', 30001);
//            }
//            $apply->name = $params['name'];
//            $apply->realname = $params['realname'];
//            $apply->company_name = $params['company_name'];
//            $apply->phone = $params['phone'];
//            $apply->province_name = $params['province_name'];
//            $apply->city_name = $params['city_name'];
//            $apply->area_name = $params['area_name'];
//            $apply->province_id = $params['province_id'];
//            $apply->city_id = $params['city_id'];
//            $apply->area_id = $params['area_id'];
//            $apply->address = $params['address'];
//            $apply->company_bank = $params['company_bank'];
//            $apply->company_bank_name = $params['company_bank_name'];
//            $apply->company_bank_card = $params['company_bank_card'];
//            $apply->company_license_image = $params['company_license_image'];
//            $apply->company_representative_idcard_front_image = $params['company_representative_idcard_front_image'];
//            $apply->company_representative_idcard_reverse_image = $params['company_representative_idcard_reverse_image'];
//            $apply->status = 0;
//            $apply->updated_at = timestamp();
//            if($apply->save()){
//                json(['code' => 1, 'msg' => '申请修改成功！']);
//                exit;
//            }else{
//                json(['code' => 0, 'msg' => '申请修改失败！']);
//                exit;
//            }
//        }catch (Exception $e){
//            json(['code' => 0, 'msg' => ($e->getCode() !== 30001)?'申请不存在':$e->getMessage()]);
//            exit;
//        }
//    }
    public function editAction()
    {
        if (!$this->isPost()){
            json(['code' => 0, 'msg' => '必须POST']);
            exit;
        }
        $params = $this->postData;
        $info = DB::table('member_zmd_apply')->where('member_id',$this->user->id)->first();
        if($info){
            if($info->type == 1){
                unset($params['area'],$params['agreement'],$params['timestamp'],$params['sign']);
                $params['updated_at'] = date('Y-m-d H:i:s');
                DB::table('member_zmd_apply')->where('id',$info->id)->update($params);
                json(['code' => 1, 'msg' => '操作成功']);
                exit;
            }
            json(['code' => 0, 'msg' => '不能重复申请']);
            exit;
        }

        if($params['type'] == 2){
            $zmd_yj_money = configModel::where('type', 1)->where('key', 'zmd_yj_money')->value('value');
            if($this->user->total_consume_num < $zmd_yj_money){
                json(['code' => 0, 'msg' => '业绩不达标不能申请']);
                exit;
            }
        }
        unset($params['area'],$params['agreement'],$params['timestamp'],$params['sign']);
        if($params['type'] == 1){
            $params['agent_fee']  = configModel::where('type', 1)->where('key', 'zmd_cg_money')->value('value');
        }else{
            $params['agent_fee_pay_status'] = 1;

            DB::table('member')->where(['id'=>$this->user->id])->update(['is_zmd'=>2]);
        }
        $params['member_id'] = $this->user->id;
        $params['created_at'] = date('Y-m-d H:i:s');

         DB::table('member_zmd_apply')->insert($params);
        json(['code' => 1, 'msg' => '申请成功']);
        exit;
    }

    #[取消申请]
    public function cancelAction()
    {
        if(!$this->isPost()){
            json(['code' => 0, 'msg' => '请求出错！']);
            exit;
        }
        $id = $this->get('id');
        try {
            $apply = \app\modules\Admin\models\dianpuapplyModel::query()->findOrFail($id);
            if($apply->member_id !== $this->user->id){
                throw new Exception('取消别人的申请', 30001);
            }
            if($apply->status !== 0){
                throw new Exception('申请已经处理，不能取消', 30001);
            }
            if($apply->delete()){
                json(['code' => 1, 'msg' => '申请取消成功']);
                exit;
            }else{
                json(['code' => 0, 'msg' => '申请取消失败，请重试']);
                exit;
            }
        }catch (Exception $e){
            json(['code' => 0, 'msg' => ($e->getCode() !== 30001)?'申请不存在':$e->getMessage()]);
            exit;
        }
    }

    #[创建申请]
    public function createAction()
    {
        if(!$this->isPost()){
            json(['code' => 0, 'msg' => '请求出错！']);
            exit;
        }
        $params = $this->postData;
        try {
            Validate::check($params, [
                'name' => [
                    'role' => 'required|maxlength:20|minlength:4',
                    'msg' => '店铺名称',
                ],
                'realname' => [
                    'role' => 'required|maxlength:10|minlength:2',
                    'msg' => '法人代表姓名',
                ],
                'company_name' => [
                    'role' => 'required|maxlength:20|minlength:2',
                    'msg' => '公司名称',
                ],
                'phone' => [
                    'role' => 'required|maxlength:20|minlength:7',
                    'msg' => '联系电话',
                ],
                'area_name' => [
                    'role' => 'required',
                    'msg' => '商家所在地区',
                ],
                'address' => [
                    'role' => 'required',
                    'msg' => '商家详细地址',
                ],
                'company_bank' => [
                    'role' => 'required',
                    'msg' => '对公帐户开户行',
                ],
                'company_bank_name' => [
                    'role' => 'required',
                    'msg' => '对公帐户名称',
                ],
                'company_bank_card' => [
                    'role' => 'required',
                    'msg' => '对公帐户帐号',
                ],
                'company_license_image' => [
                    'role' => 'required',
                    'msg' => '企业营业执照',
                ],
                'company_representative_idcard_front_image' => [
                    'role' => 'required',
                    'msg' => '法定代表人身份证头像面照片',
                ],
                'company_representative_idcard_reverse_image' => [
                    'role' => 'required',
                    'msg' => '法定代表人身份证国徽面照片',
                ],
                //                'company_representative_idcard_hand_image' => [
                //                    'role' => 'required',
                //                    'msg' => '法定代表人身份证手持照片',
                //                ],
            ]);
            if(empty($this->user->phone)){
                throw new Exception('您没有绑定手机号，请绑定手机号后再申请店铺');
            }
            if(empty($this->user->password)){
                throw new Exception('您没有设置登录密码，请设置登录密码后再申请店铺');
            }
            if(\app\modules\Admin\models\dianpuapplyModel::query()->where('member_id', '=', $this->user->id)->exists()){
                throw new Exception('申请已存在');
            }
            if(\app\modules\Admin\models\merchantModel::query()->where('member_id', '=', $this->user->id)->exists()){
                throw new Exception('已经有店铺或已经是店铺管理者');
            }
            if(
                \app\modules\Admin\models\dianpuapplyModel::query()->where('company_name', '=', $params['company_name'])->exists()
                ||
                dianpuModel::query()->where('company_name', '=', $params['company_name'])->exists()
            ){
                throw new Exception('企业名称已经被占用');
            }
            if(isset($params['name'])){
                if(
                    \app\modules\Admin\models\dianpuapplyModel::query()->where('name', '=', $params['name'])->exists()
                    ||
                    \app\modules\Admin\models\dianpuModel::query()->where('name', '=', $params['name'])->exists()
                ){
                    throw new Exception('店铺名称已经被占用');
                }
            }else{
                $params['name'] = $params['company_name'].'的店铺';
            }
            unset($params['id']);
            $this->cleanTableColumn('dianpu_apply', $params);
            $params['member_id'] = $this->user->id;
            $params['created_at'] = timestamp();
            $params['updated_at'] = $params['created_at'];
            $params['company_representative_idcard_hand_image'] = '';
            $res = DB::transaction(function () use ($params){
                return DB::table('dianpu_apply')->insert($params);
            });
            if($res){
                json(['code' => 1, 'msg' => '申请店铺代理成功，请保持联系电话畅通!']);
                exit;
            }else{
                json(['code' => 0, 'msg' => '申请提交失败，请刷新后再试！']);
                exit;
            }
        } catch (Exception $e) {
            json(['code' => 0, 'msg' => $e->getMessage()]);
            exit;
        }

    }
}